from sqlalchemy import *
from sqlalchemy.orm import scoped_session,sessionmaker#实现了上下文
from sqlalchemy.ext.declarative import declarative_base

# # 创建数据库引擎
# engine = create_engine(
#     url="mysql+pymysql://root:root@127.0.0.1:3306/flaskstudy",
#     echo=True,
#     pool_size=20,  # 连接池大小
#     pool_recycle=3600,#自动断开时间
# )
# db = sessionmaker(bind=engine)#创建会话类
#
# session = db()#创建会话对象
#
# Model = declarative_base()
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()
class BaseModel(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer,primary_key=True)
    def __init__(self,**kwargs):
        for k,v in kwargs.items():
            setattr(self,k,v)

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
    def __getitem__(self, item):
        print(item)
        return getattr(self,item)
    def __setitem__(self, key, value):
        setattr(self,key,value)

Model = declarative_base()

migrate = Migrate()
session = db.session